﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace ParsingBot
{
	class DBAccess
	{
		private SqlConnection conn;
		private SqlTransaction trans = null;
		protected string ConnectionString = "server=110.15.11.87,1433;uid=dexter;pwd=ekagud2!;database=Webtoon";
		
		public bool CheckCompletedWebtoon(string id)
		{
			SqlCommand cmd = new SqlCommand();
			cmd.CommandText = @"SELECT finished 
								  FROM WEBTOONLIST
								 WHERE ID = @id";
			bool _preOpened = true;
			if( conn == null || conn.State != ConnectionState.Open )
			{
				_preOpened = false;
				conn = new SqlConnection(this.ConnectionString);
				conn.Open();
			}
			
			cmd.Parameters.Add("@id", SqlDbType.NVarChar, 16).Value = id;
			cmd.Connection = conn;
			
			int completed = Convert.ToInt32( cmd.ExecuteScalar() );
			
			if(!_preOpened)
			{
				conn.Close();
				conn.Dispose();
			}
			if( completed == 1 )
				return true;
			
			return false;
		}
		
		public DataTable GetUpdateTimeList(string id)
		{
			SqlCommand cmd = new SqlCommand();
			cmd.CommandText = @"SELECT Time
								  FROM CARTOONLIST
								 WHERE ID = @id
								 ORDER BY Time DESC";
			bool _preOpened = true;
			if ( conn == null || conn.State != ConnectionState.Open )
			{
				_preOpened = false;
				conn = new SqlConnection(this.ConnectionString);
				conn.Open();
			}
			
			cmd.Parameters.Add("@id", SqlDbType.NVarChar, 16).Value = id;
			cmd.Connection = conn;
			
			DataSet ds = new DataSet();
			SqlDataAdapter adpt = new SqlDataAdapter(cmd);
			adpt.Fill(ds);
			adpt.Dispose();
			cmd.Dispose();
			
			if(!_preOpened)
			{
				conn.Close();
				conn.Dispose();
			}
			
			return ds.Tables[0];
		}
	}
}
